
import 'dart:math';

extension NumberExt on num? {

  String fmtPrice({int posNum = 2, bool needDiv100 = true, String defaultVal = "0"}) {
    if (this == null) return defaultVal;
    var numStr = (this! / (needDiv100 ? 100 : 1)).toString();
    if (numStr.contains(".")) {
      List<String> sp = numStr.split(".");
      numStr = sp[0];
      if (sp[1].isNotEmpty && int.parse(sp[1]) > 0&&posNum>0) {
        //取数组中最小值做为真实的位数 类似Math.min(a, b)
        int realPos = [posNum, sp[1].length].reduce(min);
        String decimalStr = sp[1].substring(0, realPos);
        if (int.parse(decimalStr) > 0) {
          numStr = numStr + ".";
          while (decimalStr.lastIndexOf("0") == decimalStr.length - 1) {
            decimalStr = decimalStr.substring(0, decimalStr.length - 1);
          }
          numStr = numStr + decimalStr;
        }
      }
    }
    return numStr;
    /*var much = List<String>.generate(posNum, (index) => "#").fold("", (pre, element) => "$pre$element");
    print("多少个#====》$much");
    return NumberFormat("#,##0.$much", "en_US").format(this);*/
  }

}